package com.lanou.student.mapper.impl;

import com.lanou.student.bean.Student;
import com.lanou.student.bean.User;
import com.lanou.student.mapper.UserMapper;
import com.lanou.student.util.ClassUtil;
import com.lanou.student.util.ExcelUtil;
import com.lanou.student.util.StringUilt;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author zyj
 * @date 2022年06月01日 9:21
 */
public class UserMapperImpl implements UserMapper {
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    static {
        try {

            File file = new File("user.xlsx");
            if (!file.exists()) {
                Workbook workbook = ExcelUtil.getWorkbook("user.xlsx");
                Sheet sheet = workbook.createSheet("user");
                Row row = sheet.createRow(0);
                ExcelUtil.setCellValue(row, "ID", "userName"
                        , "password", "eamil", "phone"
                        , "createTime", "updateTime");
                workbook.write(new FileOutputStream("user.xlsx"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public int getId() {
        List<User> users = getUsers();
        if (users.isEmpty()){
            return 1;
        }
        return users.get(users.size()-1).getId() + 1;
    }
    public boolean regester(User user) {
        //获取Workbook的对象
        user.setId(getId());
        try {
            Workbook workbook = ExcelUtil.getWorkbook("user.xlsx");
            Sheet sheet = workbook.getSheet("user");
            Row row = sheet.createRow(sheet.getLastRowNum() + 1);
            ExcelUtil.setCellValue(row, user.getId(), user.getUserName()
                    , user.getPassword(), user.getEamil(), user.getPhone()
                    , user.getCreateTime(), user.getUpdateTime());
            OutputStream os = new FileOutputStream("user.xlsx");
            os.flush();
            workbook.write(os);

            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;
    }


    /**
     * userName：null 根据email email：null，根据userName
     *
     * @param userName
     * @param email
     * @return
     */
    public User getUser(String userName, String email) {
        List<User> users = getUsers();
        for (User u:users) {
            if (Objects.equals(u.getUserName(),userName)){
                return u;
            }
            if (Objects.equals(u.getEamil(),email)){
                return u;
            }
        }
        return null;
    }

    @Override
    public boolean resetPassword(User u) {
        Workbook workbook = ExcelUtil.getWorkbook("user.xlsx");
        Sheet sheet = workbook.getSheet("user");
        int index = 0;
        List<User> users = getUsers();
        for (User user:users) {
            if (Objects.equals(user.getId(),u.getId())){
                break;
            }
            index++;
        }
        Row row = sheet.getRow(index + 1);
        row.getCell(2).setCellValue(u.getPassword());
        row.getCell(6).setCellValue(sdf.format(new Date()));
        try {
            workbook.write(new FileOutputStream("user.xlsx"));
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public List<User> getUsers() {
        List<User> users = new ArrayList<>();
        Workbook workbook = ExcelUtil.getWorkbook("user.xlsx");
        Sheet sheet = workbook.getSheet("user");
        int first = sheet.getFirstRowNum() + 1;
        int last = sheet.getLastRowNum();
        for (int i = first; i <= last; i++) {
            User user = ClassUtil.getObj(sheet.getRow(i),User.class);
            users.add(user);
        }
        return users;
    }
}
